Se lee la base de datos con formato excel con la función read_excel
Se eliminar las filas con NA: valor_mercadoNA <- is.na(X2021$valor_mercado)
Hacemos una tabla por años y por numero de inmuebles por municipio

library(data.table)

# TABLA POR AÑOS
table(testigos$anro)

2014 2015 2016 2017 2018 2019 2020 2021 2022 
   4   10  103  819  666  809  682  716   27 
# ORDEN DECRECIENTE DE MUNICIPIOS
conarmuni<-sort(decreasing = TRUE, (table(testigos$nucleopost))) 
head(conarmuni, 10)

                       MADRID            POZUELO DE ALARCON 
                          326                           207 
                  MAJADAHONDA                COLMENAR VIEJO 
                          200                           146 
           BOADILLA DEL MONTE                     GALAPAGAR 
                          135                           113 
         ROZAS DE MADRID, LAS MATAS-PINAR-MONTE ROZAS (LAS) 
                          104                            94 
             COLLADO VILLALBA                  TORRELODONES 
                           92                            92 
# library(dplyr)
# library(magrittr)
# save(testigos,file="testigos.Rda")
test <- select(testigos, nucleopost, coorde, coordn, calle)%>%
  filter(testigos$nucleopost>90)

test # Hemos seleccionados los nucleos, coordenadas y calles con municipios mayores que tengan mas de 90 testigos y los guardamos. Lo vamos a utilizar para su localizacion
## # A tibble: 2,861 x 4
##    nucleopost           coorde           coordn            calle       
##    <chr>                <chr>            <chr>             <chr>       
##  1 ROZAS DE MADRID, LAS 40,5175610066326 -3,92866357260355 SALVIA      
##  2 ROZAS DE MADRID, LAS 40,5194850930873 -3,93188145820519 LAZAREJO    
##  3 PEDREZUELA           40,7382143494648 -3,63302707625776 TARRAGONA   
##  4 PEDREZUELA           40,7459913521201 -3,59977622654704 DE LA ERMITA
##  5 VILLAVICIOSA DE ODON 40.3962299       -3.9131413        MIÑO        
##  6 VILLAVICIOSA DE ODON 40.3834628       -3.9231219        GUADIANA    
##  7 VILLAVICIOSA DE ODON 40.3816200       -3.9215000        MIÑO        
##  8 VILLAVICIOSA DE ODON 40.3849347       -3.9232046        RIANSARES   
##  9 VILLAVICIOSA DE ODON 40.3775674       -3.9322085        TAJO        
## 10 VILLAVICIOSA DE ODON 40.3787985       -3.9291633        SIL         
## # ... with 2,851 more rows
# save(test,file="test90.Rda")
# load("test90.Rda")
Eliminamos variables, columnas no utiles para el estudio y las que hay las cambiamos de nombre

DISTRIBUCION DE VALOR DE MERCADO POR TOTALES (1405 obs. de 38 variables)


LOCALIZACION DE LOS INMUEBLES CON CAPA DE CATASTRO

MODELO 1 PARA LOS DATOS TOTALES SOLO CON AÑO Y SC ADOPTADA

# install.packages("ggeffects")

library(ggeffects)
library(splines)

totaldata<-as.data.frame(total)
data(totaldata)
fit1 <- lm(valor_mercado ~ anno + S_adopt, data=totaldata)

# summary(fit1)

ggpredict(fit1, terms = "anno")
# Predicted values of valor_mercado

anno | Predicted |               95% CI
---------------------------------------
2015 |  4.13e+05 | [2.40e+05, 5.86e+05]
2016 |  2.60e+05 | [2.29e+05, 2.91e+05]
2017 |  3.24e+05 | [3.00e+05, 3.49e+05]
2018 |  2.41e+05 | [2.16e+05, 2.66e+05]
2019 |  3.29e+05 | [3.07e+05, 3.52e+05]
2020 |  2.70e+05 | [2.47e+05, 2.92e+05]
2021 |  3.58e+05 | [3.27e+05, 3.88e+05]

Adjusted for:
* S_adopt = 192.69



REPRESENTACION GRAFICA DEL MODELO 1 AJUSTADO

total<-data[data$S_adopt !=0,] # Eliminamos las filas con una Scp cero
total<-total[total$uso == "Residencial",] # Solo las Residencial
total<-total[total$S_adopt < 800,] # Eliminamos las superficies superiores a 800
total<-total[total$valor_mercado < 2000000,] # Eliminamos las superficies superiores a 800


g <- ggplot(total, aes(x = S_adopt, y = valor_mercado)) +
  geom_point(color = "dodgerblue", alpha = .5) +
  labs(x = "Superficie adoptada", y = "Valor de Mercado")

# summary(g)

g +
  geom_abline(intercept = coefficients(fit1)[1],
              slope = coefficients(fit1)[8],
              color = "darkorange2", size = 1.5) +
  labs(title = paste0("y = ", round(coefficients(fit1)[8], 2),
                      " * x + ", round(coefficients(fit1)[1], 2)))+
  geom_vline(xintercept = 600, linetype = 2, color = "red", size = 1)+
  geom_hline(yintercept = 1152850, linetype = 2, color = "red", size = 1)


PREDICCION DEL MODELO 1


EL Valor de Mercado obtenido para una vivienda de 600 m2 es de: 1.152.850,69 euros